%{
#include "y.tab.h"
#include "id.h"

extern void *yylval;

%}

%%

[a-zA-Z_][a-zA-Z_0-9]* {
              int i;

              if ((i = reserved(yytext)) != 0) {
                return i;
              }

              yylval = idCreate(yytext);
              return ID;
            }

[:\.,]       return *yytext;

%%

int yywrap()
{
  return 1;
}

int strieq(const char *s1, const char *s2)
{
  int i;
  size_t n = strlen(s1) + 1;

  for (i = 0; i < n; i++) {
    if (tolower(s1[i]) != tolower(s2[i])) {
      return 0;
    }
  }

  return 1;
}

int reserved(const char *word)
{
  if (strieq(word, "select")) {
    return SELECT;
  }
  else if (strieq(word, "from")) {
    return FROM;
  }
  else if (strieq(word, "on")) {
    return ON;
  }
  else if (strieq(word, "non")) {
    return NON;
  }
  else if (strieq(word, "empty")) {
    return EMPTY;
  }

  return 0;
}

